package tnviet.proj.jna.analyzer;
import jpcap.packet.*;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;

public class ARPAnalyzer extends JNPacketAnalyzer
{
    private static ResourceManager resourceManager = new ResourceManager(JNPacketAnalyzer.class, Program.locale);
	private static final String[] valueNames={
		resourceManager.getString("arp.hardwareType"),
		resourceManager.getString("arp.protocolType"),
		resourceManager.getString("arp.hardwareAddLength"),
		resourceManager.getString("arp.protocolAddLength"),
		resourceManager.getString("arp.operation"),
		resourceManager.getString("arp.sendHardwareAdd"),
		resourceManager.getString("arp.sendProtocolAdd"),
		resourceManager.getString("arp.targetProtocolAdd"),
		resourceManager.getString("arp.targetHardwareAdd"),
	};
	private ARPPacket arp;
	
	public ARPAnalyzer(){
		layer=NETWORK_LAYER;
	}
	
	public boolean isAnalyzable(Packet p){
		return (p instanceof ARPPacket);
	}
	
	public String getProtocolName(){
		return resourceManager.getString("arp/rarp");
	}
	
	public String[] getValueNames(){
		return valueNames;
	}
	
	public void analyze(Packet p){
		if(!isAnalyzable(p)) return;
		arp=(ARPPacket)p;
	}
	
	public Object getValue(String valueName){
		for(int i=0;i<valueNames.length;i++)
			if(valueNames[i].equals(valueName))
				return getValueAt(i);
		
		return null;
	}
	
	Object getValueAt(int index){
		switch(index){
			case 0: 
			switch(arp.hardtype){
				case ARPPacket.HARDTYPE_ETHER: return resourceManager.getString("arp.ethernet") + " ("+arp.hardtype+")";
				case ARPPacket.HARDTYPE_IEEE802: return resourceManager.getString("arp.tokenRing") +" ("+arp.hardtype+")";
				case ARPPacket.HARDTYPE_FRAMERELAY: return resourceManager.getString("arp.frameRelay") + " ("+arp.hardtype+")";
				default: return new Integer(arp.hardtype);
			}
			case 1:
			switch(arp.prototype){
				case ARPPacket.PROTOTYPE_IP: return resourceManager.getString("ip") + " ("+arp.prototype+")";
				default: return new Integer(arp.prototype);
			}
			case 2: return new Integer(arp.hlen);
			case 3: return new Integer(arp.plen);
			case 4:
			switch(arp.operation){
				case ARPPacket.ARP_REQUEST: return resourceManager.getString("arp.request");
				case ARPPacket.ARP_REPLY: return resourceManager.getString("arp.reply");
				case ARPPacket.RARP_REQUEST: return resourceManager.getString("rarp.request");
				case ARPPacket.RARP_REPLY: return resourceManager.getString("rarp.reply");
				case ARPPacket.INV_REQUEST: return resourceManager.getString("arp.idPeerRequest");
				case ARPPacket.INV_REPLY: return resourceManager.getString("arp.idPeerReply");
				default: return new Integer(arp.operation);
			}
			case 5: return arp.getSenderHardwareAddress();
			case 6: return arp.getSenderProtocolAddress();
			case 7: return arp.getTargetHardwareAddress();
			case 8: return arp.getTargetProtocolAddress();
			default: return null;
		}
	}
	
	public Object[] getValues(){
		Object[] v=new Object[valueNames.length];
		for(int i=0;i<valueNames.length;i++)
			v[i]=getValueAt(i);
		
		return v;
	}

    @Override
    public String getValueNameAt(int index) {
        return valueNames[index];
    }
}
